<?php

use App\Models\User;
use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);

        //头像假数据
        $avatars = [
            'http://bbs.laravel.com//uploads/images/avatars/201911/18/1_1574047105_v1cy7b74aP.jpeg',
            'http://bbs.laravel.com//uploads/images/avatars/201911/19/1_1574131860_gljCHJeJBJ.jpeg',
            'http://bbs.laravel.com//uploads/images/avatars/201911/19/1_1574131880_CvQwO7pNNk.jpeg',
            'http://bbs.laravel.com//uploads/images/avatars/201911/19/1_1574131898_oUSSs5iXdl.jpeg',
            'http://bbs.laravel.com//uploads/images/avatars/201911/19/1_1574131913_HZzcp7YFJB.jpeg'
        ];

        // 生成数据集合
        $users = factory(User::class)
            ->times(10)
            ->make()
            ->each(function ($user, $index)
            use ($faker, $avatars) {
                // 从头像数组中随机取出一个并赋值
                $user->avatar = $faker->randomElement($avatars);
            });

        // 让隐藏字段可见，并将数据集合转换为数组
        $user_array = $users->makeVisible(['password', 'remember_token'])->toArray();

        // 插入到数据库中
        User::insert($user_array);

        $user = User::find(1);
        $user->name = '境界的彼方';
        $user->email = '822041817@qq.com';
        $user->avatar = 'http://bbs.laravel.com//uploads/images/avatars/201911/18/1_1574047105_v1cy7b74aP.jpeg';
        $user->save();

        // 初始化用户角色,将1号用户指派为[站长]
        $user->assignRole('Founder');

        //将2号用户设置为[管理员]
        $user = User::find(2);
        $user->assignRole('Maintainer');
    }
}
